<template>
  <div class="register-page">
    <van-nav-bar title="面经登录" />
    <div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button type="info" block native-type="submit">提交</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { save } from '@/utils/storage'
export default {
  name: 'LoginPage',
  props: {},
  components: {},
  data () {
    return {
      username: '',
      password: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onSubmit () {
      try {
        // 发送请求
        const { data } = await login({
          username: this.username,
          password: this.password
        })

        // 用户登陆成功后把token存入localstory里面,下次就用登录了
        save('TOKEN', data.data.token)
        this.$router.push('/layout')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
